#!/usr/bin/env python
# _*_ coding: utf-8 _*_
#
#
# @File : simple_mnist.py.py
# @Time : 2020-10-13 21:06 
# Copyright (C) 2020 WeiKeting<weikting@gmail.com>. All rights reserved.
# @Description :
#
#

import tensorflow as tf
import numpy as np

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

# 构建神经网络模型
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128),
    tf.keras.layers.LeakyReLU(),
    tf.keras.layers.Dense(10),
    tf.keras.layers.Softmax()
])

# 训练配置
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# 训练
model.fit(x_train, y_train, batch_size=60, epochs=8)

# 用测试集评估
model.evaluate(x_test, y_test, verbose=2)

# 预测图片类别
print(np.argmax(model.predict(x_test[:8]), axis=-1), y_test[:8])
